using System;
using System.ComponentModel;
using System.Collections;
using System.Text.RegularExpressions;
using System.Xml.Serialization;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Data;
using System.Configuration;
using Microsoft.SharePoint.Administration;
using System.Xml;
using System.Web;


namespace KbWorksBlogMigrationtool
{
    class ListsProxy2010
    {
        Logging log = new Logging();
        private bool _ListMigreren;

        public bool ListMigreren
        {
            get { return _ListMigreren; }
            set { _ListMigreren = value; }
        }

        public void Createlist(XmlNode OldList, string url)
        {
            //check if list exists
            bool listExist = false;
            int listCount = 0;
            string guid = string.Empty;
            string featureUrl = string.Empty;
            SPSite site = new SPSite(url);//
            SPWeb web = site.OpenWeb();//makes connection and opens the site to allow modification
            string oldListTitle = OldList.Attributes["Title"].Value;
            ListMigreren = true;

            while (listCount < web.Lists.Count)
            {

                if (web.Lists[listCount].Title == oldListTitle)
                {
                    listExist = true;
                    break;
                }
                // Console.WriteLine(web.Lists[listCount].Title);
                listCount++;
            }
            //don't copy all lists since they are made by EBE again.
            //if (listExist == false)
            //{
            //    switch (oldListTitle)
            //    {
            //        case "Linkbacks Received":
            //            listExist = true;
            //            break;
            //        case "LinkbacksSent":
            //            listExist = true;
            //            break;
            //        case "Media":
            //            listExist = true;
            //            break;
            //        case "Other Blogs":
            //            listExist = true;
            //            break;
            //        case "Themes":
            //            listExist = true;
            //            break;
            //    }
            //}
            if (listExist == false)
            {

                try
                {
                    string defaultUrl = OldList.Attributes["DefaultViewUrl"].Value;
                    string[] defaultViewUrl = defaultUrl.ToLower().Split(new Char[] { '/' });
                    //Uri  = new Uri(defaultUrl);
                    int segment = 0;
                    segment = defaultViewUrl.GetUpperBound(0);
                    if (defaultUrl.ToLower().EndsWith(".aspx"))
                    {
                        featureUrl = defaultViewUrl[segment - 1].ToString();
                    }
                    else
                    {
                        featureUrl = defaultViewUrl[segment].ToString();
                    }

                    SPList list = null;

                    switch (OldList.Attributes["ServerTemplate"].Value)
                    {
                        //oldtemplate type = 100
                        case "100":
                            log.append("create list voor url " + url + " list100 description" + OldList.Attributes["Description"].Value + "type list is GenericList", 4, "List");
                            System.Guid list100 = web.Lists.Add(OldList.Attributes["Title"].Value,
                                OldList.Attributes["Description"].Value, SPListTemplateType.GenericList);
                            list = web.Lists[list100];
                            break;
                        case "101":
                            log.append("create list voor url " + url + " list101 description" + OldList.Attributes["Description"].Value + "type list is DocumentLibrary", 4, "List");

                            System.Guid list101 = web.Lists.Add(OldList.Attributes["Title"].Value,
                                OldList.Attributes["Description"].Value, SPListTemplateType.DocumentLibrary);
                            list = web.Lists[list101];
                            break;
                        case "102":
                            log.append("create list voor url " + url + " list102 description" + OldList.Attributes["Description"].Value + "type list is Survey", 4, "List");

                            System.Guid list102 = web.Lists.Add(OldList.Attributes["Title"].Value,
                                OldList.Attributes["Description"].Value, SPListTemplateType.Survey);
                            list = web.Lists[list102];
                            break;
                        case "103":
                            log.append("create list voor url " + url + " list103 description" + OldList.Attributes["Description"].Value + "type list is Links", 4, "List");

                            System.Guid list103 = web.Lists.Add(OldList.Attributes["Title"].Value,
                                OldList.Attributes["Description"].Value, SPListTemplateType.Links);
                            list = web.Lists[list103];
                            break;
                        case "104":
                            log.append("create list voor url " + url + " list104 description" + OldList.Attributes["Description"].Value + "type list is Announcements", 4, "List");

                            System.Guid list104 = web.Lists.Add(OldList.Attributes["Title"].Value,
                                OldList.Attributes["Description"].Value, SPListTemplateType.Announcements);
                            list = web.Lists[list104];
                            break;
                        case "105":
                            log.append("create list voor url " + url + " list105 description" + OldList.Attributes["Description"].Value + "type list is Contacts", 4, "List");

                            System.Guid list105 = web.Lists.Add(OldList.Attributes["Title"].Value,
                                OldList.Attributes["Description"].Value, SPListTemplateType.Contacts);
                            list = web.Lists[list105];
                            break;
                        case "106":
                            log.append("create list voor url " + url + " list106 description" + OldList.Attributes["Description"].Value + "type list is Events", 4, "List");

                            System.Guid list106 = web.Lists.Add(OldList.Attributes["Title"].Value,
                                OldList.Attributes["Description"].Value, SPListTemplateType.Events);
                            list = web.Lists[list106];
                            break;
                        case "107":
                            log.append("create list voor url " + url + " list107 description" + OldList.Attributes["Description"].Value + "type list is Tasks", 4, "List");

                            System.Guid list107 = web.Lists.Add(OldList.Attributes["Title"].Value,
                                OldList.Attributes["Description"].Value, SPListTemplateType.Tasks);
                            list = web.Lists[list107];
                            break;
                        case "108":
                            log.append("create list voor url " + url + " list108 description" + OldList.Attributes["Description"].Value + "type list is DiscussionBoard", 4, "List");

                            System.Guid list108 = web.Lists.Add(OldList.Attributes["Title"].Value,
                                OldList.Attributes["Description"].Value, SPListTemplateType.DiscussionBoard);
                            list = web.Lists[list108];
                            break;
                        case "109":
                            log.append("create list voor url " + url + " list109 description" + OldList.Attributes["Description"].Value + "type list is Picturelibrary", 4, "List");

                            System.Guid list109 = web.Lists.Add(OldList.Attributes["Title"].Value,
                                OldList.Attributes["Description"].Value, SPListTemplateType.PictureLibrary);
                            list = web.Lists[list109];
                            break;
                        case "110":
                            log.append("create list voor url " + url + " list110 description" + OldList.Attributes["Description"].Value + "type list is Datasources", 4, "List");

                            System.Guid list110 = web.Lists.Add(OldList.Attributes["Title"].Value,
                                OldList.Attributes["Description"].Value, SPListTemplateType.DataSources);
                            list = web.Lists[list110];
                            break;
                        case "111":
                            log.append("create list voor url " + url + " list111 description" + OldList.Attributes["Description"].Value + "type list is WebTemplateCatalog", 4, "List");

                            System.Guid list111 = web.Lists.Add(OldList.Attributes["Title"].Value,
                                OldList.Attributes["Description"].Value, SPListTemplateType.WebTemplateCatalog);
                            list = web.Lists[list111];
                            break;
                        case "113":
                            log.append("create list voor url " + url + " list113 description" + OldList.Attributes["Description"].Value + "type list is WebPartCatalog", 4, "List");

                            System.Guid list113 = web.Lists.Add(OldList.Attributes["Title"].Value,
                                OldList.Attributes["Description"].Value, SPListTemplateType.WebPartCatalog);
                            list = web.Lists[list113];
                            break;
                        case "114":
                            log.append("create list voor url " + url + " list114 description" + OldList.Attributes["Description"].Value + "type list is ListemplateCatalog", 4, "List");

                            System.Guid list114 = web.Lists.Add(OldList.Attributes["Title"].Value,
                                OldList.Attributes["Description"].Value, SPListTemplateType.ListTemplateCatalog);
                            list = web.Lists[list114];
                            break;
                        case "115":
                            log.append("create list voor url " + url + " list115 description" + OldList.Attributes["Description"].Value + "type list is Xmlform", 4, "List");

                            System.Guid list115 = web.Lists.Add(OldList.Attributes["Title"].Value,
                                OldList.Attributes["Description"].Value, SPListTemplateType.XMLForm);
                            list = web.Lists[list115];
                            break;
                        case "120":
                            log.append("create list voor url " + url + " list120 description" + OldList.Attributes["Description"].Value + "type list is Custom grid", 4, "List");

                            System.Guid list120 = web.Lists.Add(OldList.Attributes["Title"].Value,
                                OldList.Attributes["Description"].Value, SPListTemplateType.CustomGrid);
                            list = web.Lists[list120];
                            break;
                        //case "1100":
                        //    log.append("create list voor url " + url + " list1100 description" + OldList.Attributes["Description"].Value + "type list is Issue tracking", 4, "List");

                        //    System.Guid list1100 = web.Lists.Add(OldList.Attributes["Title"].Value,
                        //        OldList.Attributes["Description"].Value, SPListTemplateType.IssueTracking);
                        //    list = web.Lists[list1100];
                        //    break;

                        //case "4103":
                        //    log.append("create list voor url " + url + " list4103 description" + OldList.Attributes["Description"].Value + "type list is Links", 4, "List");

                        //    //links
                        //    System.Guid list4103 = web.Lists.Add(OldList.Attributes["Title"].Value,
                        //        OldList.Attributes["Description"].Value, SPListTemplateType.Links);
                        //    list = web.Lists[list4103];
                        //    break;
                        //case "4104":
                        //    //news
                        //    log.append("create list voor url " + url + " list4104 description" + OldList.Attributes["Description"].Value + "type list is News", 4, "List");

                        //    System.Guid list4104 = web.Lists.Add(oldListTitle,
                        //        OldList.Attributes["Description"].Value, SPListTemplateType.Announcements);
                        //    list = web.Lists[list4104];
                        //    break;
                        //case "4105":
                        //    log.append("create list voor url " + url + " list4105 description" + OldList.Attributes["Description"].Value + "type list is Upload documents", 4, "List");

                        //    //upload documents custom
                        //    if (web.Features[(new Guid("{D8CECC79-4E7D-4433-A7EF-0B5B840C3E94}"))] == null)
                        //    {
                        //        web.Features.Add(new Guid("{D8CECC79-4E7D-4433-A7EF-0B5B840C3E94}"));
                        //    }
                        //    guid = "{D8CECC79-4E7D-4433-A7EF-0B5B840C3E94}";
                        //   // featureUrl = "UploadDocuments" + OldList.Attributes["Title"].Value;
                        //    System.Guid list4105 = web.Lists.Add(OldList.Attributes["Title"].Value,
                        //        OldList.Attributes["Description"].Value,featureUrl,guid,20 ,"100",SPListTemplate.QuickLaunchOptions.Off);

                        //    list = web.Lists[list4105];
                        //    break;
                        //case "4106":
                        //    //upload documents v2 custom
                        //    log.append("create list voor url " + url + " list4106 description" + OldList.Attributes["Description"].Value + "type list is Upload documents", 4, "List");

                        //    if (web.Features[(new Guid("{D8CECC79-4E7D-4433-A7EF-0B5B840C3E94}"))] == null)
                        //    {
                        //        web.Features.Add(new Guid("{D8CECC79-4E7D-4433-A7EF-0B5B840C3E94}"));
                        //    } 
                        //    guid = "{D8CECC79-4E7D-4433-A7EF-0B5B840C3E94}";
                        //    //featureUrl = "UploadDocuments" + OldList.Attributes["Title"].Value;
                        //    System.Guid list4106 = web.Lists.Add(OldList.Attributes["Title"].Value,
                        //        OldList.Attributes["Description"].Value, featureUrl, guid, 20, "100", SPListTemplate.QuickLaunchOptions.Off);
                        //    list = web.Lists[list4106];
                        //    break;
                        //case "4107":
                        //    log.append("create list voor url " + url + " list4107 description" + OldList.Attributes["Description"].Value + "type list is signup list", 4, "List");

                        //    if (web.Features[(new Guid("{7E0145F1-FE73-4fca-918A-A8D1ABAFE840}"))] == null)
                        //    {
                        //        web.Features.Add(new Guid("{7E0145F1-FE73-4fca-918A-A8D1ABAFE840}"));
                        //    }

                        //    //signup lists
                        //    guid = "{7E0145F1-FE73-4fca-918A-A8D1ABAFE840}";
                        //    //featureUrl = "Signup List" + OldList.Attributes["Title"].Value;
                        //    System.Guid list4107 = web.Lists.Add(OldList.Attributes["Title"].Value,
                        //        OldList.Attributes["Description"].Value, featureUrl, guid, 20, "100", SPListTemplate.QuickLaunchOptions.Off);

                        //    list = web.Lists[list4107];
                        //    break;
                    }
                    if (list != null)
                    {
                        list.Direction = OldList.Attributes["Direction"].Value;
                        list.ReadSecurity = Convert.ToInt32(OldList.Attributes["ReadSecurity"].Value);
                        list.WriteSecurity = Convert.ToInt32(OldList.Attributes["WriteSecurity"].Value);
                        list.AllowDeletion = Convert.ToBoolean(OldList.Attributes["AllowDeletion"].Value);
                        list.AllowMultiResponses = Convert.ToBoolean(OldList.Attributes["AllowMultiResponses"].Value);
                        // list.EnableAttachments = Convert.ToBoolean(OldList.Attributes["EnableAttachments"].Value);
                        list.EnableModeration = Convert.ToBoolean(OldList.Attributes["EnableModeration"].Value);
                        list.EnableVersioning = Convert.ToBoolean(OldList.Attributes["EnableVersioning"].Value);
                        list.Hidden = Convert.ToBoolean(OldList.Attributes["Hidden"].Value);
                        list.MultipleDataList = Convert.ToBoolean(OldList.Attributes["MultipleDataList"].Value);
                        //list.Ordered = Convert.ToBoolean(OldList.Attributes["Ordered"].Value);
                        list.ShowUser = Convert.ToBoolean(OldList.Attributes["ShowUser"].Value);

                        list.Update();
                    }
                }
                catch (Exception ex)
                {
                    log.append("unable to create list " + OldList.Attributes["Title"].Value + ex.ToString(), 4, "Error");

                }
            }
            site.Dispose();
            web.Dispose();
        }
        //private string RemoveHTML(string strText)
        //{

        //    string TAGLIST
        //          = ";!--;!DOCTYPE;A;ACRONYM;ADDRESS;APPLET;AREA;B;BASE;BASEFONT;" +
        //            "BGSOUND;BIG;BLOCKQUOTE;BODY;BR;BUTTON;CAPTION;CENTER;CITE;CODE;" +
        //            "COL;COLGROUP;COMMENT;DD;DEL;DFN;DIR;DIV;DL;DT;EM;EMBED;FIELDSET;" +
        //            "FONT;FORM;FRAME;FRAMESET;HEAD;H1;H2;H3;H4;H5;H6;HR;HTML;I;IFRAME;IMG;" +
        //            "INPUT;INS;ISINDEX;KBD;LABEL;LAYER;LAGEND;LI;LINK;LISTING;MAP;MARQUEE;" +
        //            "MENU;META;NOBR;NOFRAMES;NOSCRIPT;OBJECT;OL;OPTION;P;PARAM;PLAINTEXT;" +
        //            "PRE;Q;S;SAMP;SCRIPT;SELECT;SMALL;SPAN;STRIKE;STRONG;STYLE;SUB;SUP;" +
        //            "TABLE;TBODY;TD;TEXTAREA;TFOOT;TH;THEAD;TITLE;TR;TT;U;UL;VAR;WBR;XMP;";

        //    const string BLOCKTAGLIST = ";APPLET;EMBED;FRAMESET;HEAD;NOFRAMES;NOSCRIPT;OBJECT;SCRIPT;STYLE;";

        //    int nPos1 = 0;
        //    int nPos2 = 0;
        //    int nPos3 = 0;
        //    string strResult = "";
        //    string strTagName = "";
        //    bool bRemove;
        //    bool bSearchForBlock;

        //    nPos1 = strText.IndexOf("<");
        //    while (nPos1 >= 0)
        //    {
        //        nPos2 = strText.IndexOf(">", nPos1 + 1);
        //        if (nPos2 >= 0)
        //        {
        //            strTagName = strText.Substring(nPos1 + 1, nPos2 - nPos1 - 1);

        //            strTagName = strTagName.Replace("\r", " ").Replace("\n", " ");

        //            nPos3 = strTagName.IndexOf(" ");

        //            if (nPos3 > 0) strTagName = strTagName.Substring(0, nPos3 - 1);

        //            if (strTagName.Substring(0, 1) == "/")
        //            {
        //                strTagName = strTagName.Substring(1);
        //                bSearchForBlock = false;
        //            }
        //            else bSearchForBlock = true;

        //            if (TAGLIST.IndexOf(";" + strTagName.ToUpper() + ";", 0) > 0)
        //            {
        //                bRemove = true;
        //                if (bSearchForBlock)
        //                {
        //                    if (BLOCKTAGLIST.IndexOf(";" + strTagName.ToUpper() + ";") > 0)
        //                    {
        //                        nPos2 = strText.Length;
        //                        nPos3 = strText.IndexOf("</" + strTagName, nPos1 + 1);
        //                        if (nPos3 > 0) nPos3 = strText.IndexOf(">", nPos3 + 1);
        //                        if (nPos3 > 0) nPos2 = nPos3;
        //                    }
        //                }
        //            }
        //            else bRemove = false;

        //            if (bRemove)
        //            {
        //                strResult = strResult + strText.Substring(0, nPos1);
        //                strText = strText.Substring(nPos2 + 1);
        //            }
        //            else
        //            {
        //                strResult = strResult + strText.Substring(nPos1);
        //                strText = strText.Substring(nPos1 + 1);
        //            }
        //        }
        //        else
        //        {
        //            strResult = strResult + strText;
        //            strText = "";
        //        }

        //        nPos1 = strText.IndexOf("<");
        //    }
        //    strResult = strResult + strText;

        //    strResult = strResult.Replace("\r\n\r\n", "\r\n");

        //    return strResult;
        //} 

    }
}
